/*
    起点福利中心自动刷章节卡的脚本, ref : <https://github.com/pzx521521/qidian_AutoLookAD>
    NOTE:
        1. 适配 v7.9.270
        2. 更新日期 : 2025-01-24 18:55:02
 */

importClass(android.content.Context);
importClass(android.provider.Settings);
try {
    var enabledServices = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
    log('当前已启用的辅助服务\n', enabledServices);
    var Services = enabledServices + ":com.script.main/com.stardust.autojs.core.accessibility.AccessibilityService";
    Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, Services);
    Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, '1');
    toastLog("成功开启AutoJS的辅助服务");
} catch (error) {
    //授权方法：开启usb调试并使用adb工具连接手机，执行 adb shell pm grant com.script.main android.permission.WRITE_SECURE_SETTINGS
    toastLog("\n请确保已给予 WRITE_SECURE_SETTINGS 权限\n\n授权代码已复制，请使用adb工具连接手机执行(重启不失效)\n\n", error);
    setClip("adb shell pm grant com.script.main android.permission.WRITE_SECURE_SETTINGS");
}

// 请求屏幕截图权限
if (!requestScreenCapture()) {
    toast("请求屏幕截图权限失败");
    exit();
}

function FindIconInScreen(iconImage_1, iconImage_2) {
    let screenImage = captureScreen();
    if (!screenImage) {
        toast("屏幕截图失败");
        screenImage.recycle();
        exit();
    }

    // console.log( files.read("/sdcard/.1_video/close.png") )
    // 使用 images.findImage 函数在屏幕截图中查找模板图标
    let findResult = images.findImage(screenImage, iconImage_1, {
        threshold: 0.68, // 匹配相似度阈值，范围 0 - 1，值越大越严格
        region: [0, 0, screenImage.width, screenImage.height / 3], // 查找区域，这里设置为上半屏
        step: 1 // 查找步长，默认为 1
    });

    if(!findResult) {
        let findResult = images.findImage(screenImage, iconImage_2, {
            threshold: 0.68, // 匹配相似度阈值，范围 0 - 1，值越大越严格
            region: [0, 0, screenImage.width, screenImage.height / 3], // 查找区域，这里设置为上半屏
            step: 1 // 查找步长，默认为 1
        });
        console.log( "使用 iconImage_2" )
    }

    console.log(findResult)

    return findResult
}


function MyClick(str) {
    var mBtn = undefined
    mBtn = text(str).findOnce()
    if (mBtn != undefined) {
        mBtn = mBtn.parent();
        toastLog("找到了 : '" + str + "'")
        mBtn.click()
    }
}


function CloseAds(iconImage_1, iconImage_2) {
    findResult = FindIconInScreen(iconImage_1, iconImage_2);
    if (findResult) {
        toast("成功找到图标:" + " x:" + findResult.x + ", y:" + findResult.y);
        // 可以在这里执行对找到图标的操作，比如点击
        click(findResult.x, findResult.y)
    } else {
        toast("未找到指定图标:");
    }
}

back()
sleep(100)
back()
sleep(100)
back()
sleep(100)
back()
back()
shell("am force-stop 'com.qidian.QDReader'")

sleep(1000)

home()

if (launchApp("起点读书")) {
    toastLog("启动起点读书成功")
} else {
    toastLog("启动起点读书失败")
}

sleep(1750)

var firstScreenSkip = textMatches("跳过").findOnce();
if (firstScreenSkip != undefined) {
    toastLog("跳过首屏广告")
    firstScreenSkip.click()  // 不起作用...
    sleep(1500)
} else {
    toastLog("等待首屏广告自动结束")
    sleep(8500)   // 跳过首屏广告
}

back() // 关闭主页弹窗
var tmpClose = idMatches("imageClose").find()
if (tmpClose != undefined) {
    tmpClose.click()
    toastLog("关闭主页弹窗 OK")
} else {
    toastLog("未找到 关闭主页弹窗")
}
sleep(1500)

// 关闭升级弹窗
var upgradeBtn = id("upgrade_title").findOnce()
if (upgradeBtn != undefined) {
    back()
    toastLog("关闭升级弹窗 OK")
} else {
    toastLog("未找到 升级弹窗")
}
sleep(800)

//找到我
MyClick("我")
sleep(2500)

var warnBtn = textMatches("我知道了").findOnce();
if (warnBtn != undefined) {
    toastLog("关闭 : '青少年模式' 警告")
    // warnBtn.click()  // 不起作用...
    back()
    sleep(1500)
}

closeIconPath_1 = "https://gitee.com/think3r/learn_autoxJs/raw/master/qidianCloseAds.png";
closeIconPath_2 = "https://gitee.com/think3r/learn_autoxJs/raw/master/qidianCloseAds_2.png";
// read 本地读取, load 远程读取
var iconImage_1 = images.load(closeIconPath_1)
var iconImage_2 = images.load(closeIconPath_2)


//看视频 1~8 + 1 + 3 + 1
for (let index = 1; index < 15; index ++) {
    MyClick("福利中心")
    sleep(5000)

    let caption = "第" + parseInt(index) + "个"
    var mWatchVideoBtn = undefined

    // 执行滑动操作
    swipe(device.width / 2, device.height * 0.7, device.width / 2, device.height * 0.4, 300);
    sleep(1000); // 等待页面滑动完成

    mWatchVideoBtn =textMatches("看视频|看第\\d+个视频|看视频领福利").findOne(2000);
    if (mWatchVideoBtn == undefined) {
        toastLog("=====>>>> " + index + " --->" + caption + "未找到")
        // toastLog.log(text())
        sleep(1000)
        continue
    }
    caption = mWatchVideoBtn.text()
    toastLog("=====>>>> idx " + index + " : " + caption)

    mWatchVideoBtn.click()
    toastLog("看视频等待 11s ---> " + " : " + index)
    sleep(11000)

    sleep(50000)
    CloseAds(iconImage_1, iconImage_2)

    toastLog("查找 '开心收下/我知道了'")
    sleep(1500)
    back()
    toastLog("看视频结束: " + index + "  <-----")
    sleep(1500)
}

// ----------------------------------签到----------------------------------

toastLog("退出看视频")
back()
sleep(1000)

MyClick("书架")
sleep(1500)


var mButton = id("btnCheckIn").findOnce();
if (mButton != undefined) {
    mButton.click()
    toastLog("签到成功~")
    sleep(1000)
} else {
    toastLog("签到失败 !!!!")
}

back()

if (iconImage_1) {
    iconImage_1.recycle();
}
if (iconImage_2) {
    iconImage_2.recycle();
}
